from game_plug_in.LogitechDriver.logitech import Logitech

from pynput import mouse
from pynput import keyboard
import time
is_mouse_left = False
is_mouse_right = False
mouse_aim = (10, 5)
flag_lock_obj = True
shot_interval_time = 0.1

# def on_move(x, y):
#     print('Pointer moved to {0}'.format(
#         (x, y)))



def on_press(key):
    global flag_lock_obj
    if key == keyboard.Key.home:
        flag_lock_obj = not flag_lock_obj
        print(f'锁定功能：{"开" if flag_lock_obj else "关"}')

    print(key)
def on_click(x, y, button, pressed):
    global is_mouse_left, is_mouse_right, mouse_aim
    if pressed:
        if button == mouse.Button.left:
            is_mouse_left = True
        elif button == mouse.Button.right:
            is_mouse_right = True
        elif button == mouse.Button.x1:
            mouse_aim = (mouse_aim[0] + 100, mouse_aim[1])
            print(f'mouse_aim:{mouse_aim}')
        elif button == mouse.Button.x2:
            mouse_aim = (mouse_aim[0] - 100, mouse_aim[1])
            print(f'mouse_aim:{mouse_aim}')
    else:
        if button == mouse.Button.left:
            is_mouse_left = False
        elif button == mouse.Button.right:
            is_mouse_right = False
    print('{0} at {1}'.format('Pressed' if pressed else 'Released', (x, y)))
    print(f'is_mouse_left:{is_mouse_left}  is_mouse_right:{is_mouse_right}')


# def on_scroll(x, y, dx, dy):
#     print('Scrolled {0} at {1}'.format(
#         'down' if dy < 0 else 'up',
#         (x, y)))

# ...or, in a non-blocking fashion:
listener_mouse = mouse.Listener(on_click=on_click)
listener_mouse.start()
listener_keyboard = keyboard.Listener(on_press=on_press)
listener_keyboard.start()

while True:
    if is_mouse_right and is_mouse_left and flag_lock_obj:
        Logitech.mouse.move(mouse_aim[0], mouse_aim[1])
        time.sleep(shot_interval_time)